<?php


namespace App\Services\Repo;

use App\Models\Group;
use App\Models\Admin;
use Illuminate\Support\Facades\Log;
use Logger;
use App\Services\HelpService;
use Illuminate\Support\Facades\Auth;
use App\Services\Interfaces\RepoServiceInterface;
use Illuminate\Database\Eloquent\Model;

class AdminRepo implements RepoServiceInterface
{
    /**
     * @param array|null $_options
     * @return Model
     * <code>
     *  $repo->create(['name' => '']);
     * </code>
     */
    public function create(array $_options = null):Model
    {
        $user = new Admin();
        $user->role = 2;
        $user->email = $_options['email'];
        $user->groupRole = intval($_options['groupRole']);
        $user->group_id = $_options['group_id'];
        $user->name = $_options['name'];
        $user->company_id = $_options['company_id'];
//        $randomPwd = HelpService::createRandomStr(6);
//        $user->password = bcrypt($randomPwd);
        $user->password = bcrypt($_options['password']);
        $user->mobile = $_options['mobile'];
        if(isset($_options['position'])){
            $user->position = $_options['position'];
        }else {
            $user->position = '';
        }
        $user->save();
        Log::useFiles(storage_path('logs/pwd.txt', 'debug'));
        Log::info('['.$user->id.'] create password: ('.$_options['password'].') of ('.$user->name.')');
        return $user;
    }

    public function read($limit = 0)
    {
        //$inputs = request()->input();
        $inputs = HelpService::get_filter();
        $user = Auth::User();
        if(intval($user->role) == 1){
            $users = User::withTrashed()->search($inputs)->orderBy('created_at', 'desc')->get();
            return $users;
        }
        if($user->role == 2 && $user->groupRole == 2){
            //获取当前登录用户直属下级组织所属的账号
            $group = new Group();
            $groups = $group->all();
            //获取当前登录所属group_id的直系下属
            $group_data = $group->getTree($groups, $user->group_id);
            $group_ids = [];
            foreach ($group_data as $v) {
                $group_ids[] = $v['id'];
            }
            if (!empty($inputs)) {
                // 在有search的搜索条件时, whereIn和orWhere不能同时用,所以分开查
                // 搜索管理员
                $user_gl = User::withTrashed()->search($inputs)->whereIn('group_id', $group_ids)->orderBy('created_at', 'desc')->get();
                // 搜索操作员
                $user_cz = User::withTrashed()->search($inputs)->where(['company_id'=>$user->company_id,'role'=>2,'groupRole'=>3])
                    ->orderBy('created_at', 'desc')->get();
                $coll = $user_gl->merge($user_cz);
                return $coll;
            } else {
                $users = User::withTrashed()->search($inputs)->whereIn('group_id', $group_ids)->orderBy('created_at', 'desc')
                    ->orWhere(['company_id'=>$user->company_id,'role'=>2,'groupRole'=>3])->get();
                return $users;
            }
        }
    }

    public function find(array $_filter):Model
    {
        $user = User::find($_filter)->first();
        if (!empty($user->group_id)) {
            $group_id = $user->group_id;
            $group = Group::find($group_id);
            $group_name = $group->name;
            $user->group_name = $group_name;
            return $user;
        } else {
            return $user;
        }
    }

    public function update(array $_filter, array $_update_data):bool
    {
        $user = Admin::find($_filter)->first();
        if(!is_null($user)){
            $user->updated_at = time();
            $attr = $user->getAttributes();
            foreach($attr as $k=>$v){
                if(isset($_update_data[$k])){
                    $user->$k = $_update_data[$k];
                }
            }
            return $user->save();
        }else{
            return false;
        }
    }

    public function delete(array $_filter):bool
    {
        // TODO: Implement delete() method.
    }
}